/**
 * Created by WT on 2017/4/5.
 */
$(function () {
    var PROJECT_TYPE=CONSTANT.DictionaryTypeDefConstants.PROJECT_TYPE;//项目类型
    var PROJECT_LEVEL=CONSTANT.DictionaryTypeDefConstants.PROJECT_LEVEL;//项目级别
    var PROJECT_SUPERVISION_LEVEL=CONSTANT.DictionaryTypeDefConstants.PROJECT_SUPERVISION_LEVEL;//项目监管级别
    queryComboData(PROJECT_TYPE,'projectTypeQuery');
    queryComboData(PROJECT_LEVEL,'projectLevelQuery');
    queryComboData(PROJECT_SUPERVISION_LEVEL,'supervisionLevelQuery');
    //初始化部门下拉树
    $('.parentDepartmentId').combotree({
        url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0',
        required: true
    });

    $('#btn_save').on('click', function () {
        submitForm();
    });
    //打开人员选择弹窗
    $('#projectDirector').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function (e) {
                WindowManager.openLayer({
                    type: 2,
                    title: '选择人员信息',
                    area: ['1150px', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:function(data){
                        getSelectedUser(data);
                    }
                });
            }
        }]
    });
});
/**
 * 人员信息选择
 */
var getSelectedUser = function (data) {
    $('#projectDirector').textbox('setValue', data[0].realName);//项目负责人名称
    $('#directorId').val(data[0].id);//项目负责人ID
}
/**
 * 提交表单
 */
function submitForm() {
    $('#projectInfo').form('submit', {
        url: ROOT_PATH + '/threeSimultaneities/projectInfo/edit.shtml',
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    WindowManager.closeLayer({
                        returnValue: true
                    });
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}
/**
 * 获取下拉数据
 * @param code
 * @param queryId
 */
function queryComboData(code,queryId) {
    var pageId='#'+queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(pageId).combobox({
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200'
            });
        }
    });
}